{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "# 定语从句语法\n",
    "grammar = '''\n",
    "战斗 => 施法  ， 结果 \n",
    "施法 => 主语 动作 技能 \n",
    "结果 => 主语 获得 效果\n",
    "主语 => 张飞 | 关羽 | 赵云 | 典韦 | 许褚 | 刘备 | 黄忠 | 曹操 | 鲁班七号 | 貂蝉\n",
    "动作 => 施放 | 使用 | 召唤 \n",
    "技能 => 一骑当千 | 单刀赴会 | 青龙偃月 | 刀锋铁骑 | 黑暗潜能 | 画地为牢 | 守护机关 | 狂兽血性 | 龙鸣 | 惊雷之龙 | 破云之龙 | 天翔之龙\n",
    "获得 => 损失 | 获得 \n",
    "效果 => 数值 状态\n",
    "数值 => 1 | 1000 |5000 | 100 \n",
    "状态 => 法力 | 生命\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 得到语法字典\n",
    "def getGrammarDict(gram, linesplit = \"\\n\", gramsplit = \"=>\"):\n",
    "    #定义字典\n",
    "    result = {}\n",
    "    for line in gram.split(linesplit):\n",
    "        # 去掉首尾空格后，如果为空则退出\n",
    "        if not line.strip(): \n",
    "            continue\n",
    "        expr, statement = line.split(gramsplit)\n",
    "        result[expr.strip()] = [i.split() for i in statement.split(\"|\")]\n",
    "    #print(result)\n",
    "    return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成句子\n",
    "def generate(gramdict, target, isEng = False):\n",
    "    if target not in gramdict: \n",
    "        return target\n",
    "    find = random.choice(gramdict[target])\n",
    "    #print(find)\n",
    "    blank = ''\n",
    "    # 如果是英文中间间隔为空格\n",
    "    if isEng: \n",
    "        blank = ' '\n",
    "    return blank.join(generate(gramdict, t, isEng) for t in find)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'战斗': [['施法', '，', '结果']], '施法': [['主语', '动作', '技能']], '结果': [['主语', '获得', '效果']], '主语': [['张飞'], ['关羽'], ['赵云'], ['典韦'], ['许褚'], ['刘备'], ['黄忠'], ['曹操'], ['鲁班七号'], ['貂蝉']], '动作': [['施放'], ['使用'], ['召唤']], '技能': [['一骑当千'], ['单刀赴会'], ['青龙偃月'], ['刀锋铁骑'], ['黑暗潜能'], ['画地为牢'], ['守护机关'], ['狂兽血性'], ['龙鸣'], ['惊雷之龙'], ['破云之龙'], ['天翔之龙']], '获得': [['损失'], ['获得']], '效果': [['数值', '状态']], '数值': [['1'], ['1000'], ['5000'], ['100']], '状态': [['法力'], ['生命']]}\n",
      "gramdict的数据类型是：<class 'dict'>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "gramdict = getGrammarDict(grammar)\n",
    "print(gramdict)\n",
    "print(\"gramdict的数据类型是：{}\\n\".format(type(gramdict)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "许褚施放单刀赴会，貂蝉损失1000法力\n",
      "张飞 施放 一骑当千 ， 貂蝉 损失 100 生命\n"
     ]
    }
   ],
   "source": [
    "print(generate(gramdict,\"战斗\"))\n",
    "print(generate(gramdict,\"战斗\", True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
